// fileName      :  main.go
// createdAt     :  2021-09-27
// description   :

package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"log"
	"zl-gin/client"
	"zl-gin/redis"
	"zl-gin/utils"

	"zl-gin/config"
	"zl-gin/logger"
	"zl-gin/mysql"
	"zl-gin/router"
)

// go run -ldflags="-X main.ENV=DEV" .\main.go
var (
	// ENV 环境配置
	ENV     string = "DEV"
	VERSION string = "0.0.1"
)

func main() {

	defer func() {
		utils.GlobalTrace = nil
		if err := recover(); err != nil {
			log.Panicln(err)
		}
	}()

	//初始化配置
	config.InitConfig(ENV)

	//设置Gin运行模式
	runMode := utils.GetGinMod(ENV)
	gin.SetMode(runMode)

	//初始化Logger
	logger.InitLogger()
	defer zap.L().Sync()

	//初始化Redis
	redis.InitRedis()

	//初始化Mysql
	mysql.InitMysql()

	// eureka注册
	log.Printf("[%v] 正在注册Eureka\n", config.Get().AppName)
	client.GetInstance().RunEureka(config.Get().Eureka.Zone, config.Get().AppName, VERSION, config.Get().AppPort)

	//初始化路由
	r := router.InitRouter()
	r.Run(fmt.Sprintf(":%d", config.Get().AppPort))
}
